
var defaultCSS = "eBook_helper_background";

var tagFilters = ['SCRIPT', 'STYLE', 'INPUT', 'BUTTON', 'LINK', 'FORM'];

var doTagChange = function(o){

  // tag filter
	for(f in tagFilters){
  	if(o.tagName == tagFilters[f])
  		return;
	}
	
	// change style sheet
	if(o.style != null && o.style != ""){
		o.setAttribute("style", "");
	}
	// remove iframe
	if(o.tagName == 'IFRAME'){
		o.style.display = "none";
		return;
	}
	// remove image
	if(o.tagName == 'IMG'){
		o.style.display = "none";
		return;
	}
	// remove flash
	if(o.tagName == 'EMBED'){
		o.style.display = "none";
		return;
	}
	o.className = defaultCSS;
}

var modifyTag = function(tag){
	if(tag.tagName == "BODY"){
		tag.className = defaultCSS;
		tag.addEventListener("click", function(){}, false);
	}
	
	try{
					
		if(tag != null){
			for(var c = 0; c < tag.childNodes.length; c++){
				var o = tag.childNodes.item(c);
				if(o != null)
						doTagChange(o);
				if(o != null && o.childNodes != null && o.childNodes.length > 0)
					modifyTag(o);
			}
		}
	
	}catch(e){
		alert(e);
	}
}

chrome.extension.onRequest.addListener( 
  function(request, sender, sendResponse) {
    if (request.startTag != null && request.startTag){
    	modifyTag(document.body);
    	chrome.extension.sendRequest({'url': document.URL, 'action' : 'addUrl'}, function(){});
    }else{
    	chrome.extension.sendRequest({'url': document.URL, 'action' : 'delUrl'}, function(){});
    	location.reload();
    }
  }); 

var checkStore = function(data){
	if(data)
		modifyTag(document.body);
}

chrome.extension.sendRequest({'url': document.URL, 'action' : 'queryUrl'}, checkStore);

